﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL_NhaSach;
using BLL;

namespace BLL_NhaSach
{
    public class KhachHangBLL : IProcessData
    {
        QLNhaSachEntities db;

        public KhachHangBLL(string cnStr)
        {
            db = new QLNhaSachEntities(cnStr);
        }
        public object getDataCustom()
        {
            return (from kh in db.KhachHangs where kh.MaKH!=4 select new { kh.MaKH, kh.TenKH});
        }
        public object getAllData()
        {
            return (from kh in db.KhachHangs select new { kh.MaKH, kh.TenKH, kh.GioiTinh, kh.DiaChi, kh.SoCMND, kh.SoDienThoai, kh.Email, kh.CongNo });
        }

        public object findObjects(string find_content)
        {
            throw new NotImplementedException();
        }

        public object findObjects(int find_content)
        {
            return (db.KhachHangs.SingleOrDefault(p => p.MaKH == find_content));
        }

        public object sort(string Col_Name, bool state)
        {
            throw new NotImplementedException();
        }

        public bool addObject(object addObj)
        {
            KhachHang kh = (KhachHang)addObj;
            if (kh != null)
            {
                try
                {
                    db.KhachHangs.AddObject(kh);
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else return false;
        }

        public bool deleteObject(int id)
        {
            KhachHang kh = db.KhachHangs.SingleOrDefault(p => p.MaKH == id);
            if (kh != null)
            {
                try
                {
                    db.KhachHangs.DeleteObject(kh);
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else return false;
        }

        public bool updateObject(object updateObj)
        {
            KhachHang kh_new = (KhachHang)updateObj;
            if (kh_new != null)
            {
                try
                {
                    KhachHang kh_old = db.KhachHangs.SingleOrDefault(p => p.MaKH == kh_new.MaKH);
                    kh_old.TenKH = kh_new.TenKH;
                    kh_old.SoDienThoai = kh_new.SoDienThoai;
                    kh_old.CongNo = kh_new.CongNo;
                    kh_old.DiaChi = kh_new.DiaChi;
                    kh_old.Email = kh_new.Email;
                    kh_old.GioiTinh = kh_new.GioiTinh;
                    kh_old.SoCMND = kh_new.SoCMND;
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else return false;
        }
        public bool setCongNo(int id, decimal money)
        {
            KhachHang kh = db.KhachHangs.SingleOrDefault(p => p.MaKH == id);
            if (kh != null)
            {
                kh.CongNo += money;
                try
                {
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else return false;
        }
        public bool truCongNo(int id, decimal money)
        {
            KhachHang kh = db.KhachHangs.SingleOrDefault(p => p.MaKH == id);
            if (kh != null)
            {
                kh.CongNo -= money;
                try
                {
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else return false;
        }
    }
}
